package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import com.yzkj.cpjxc.business.domain.InitialFinancialBalance;
import com.yzkj.cpjxc.business.domain.InitialFinancialBalanceQueryVo;
import com.yzkj.cpjxc.business.service.InitialFinancialBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 财务初始余额表控制器
 */
@Controller
@RequestMapping("/initialFinancialBalance")
public class InitialFinancialBalanceController extends BaseController {

	@Autowired
	private InitialFinancialBalanceService initialFinancialBalanceService;

	@RequestMapping("/list")
	@DoLog(cnContent = "查看财务初始余额表列表信息")
	@Permission(name="initialFinancialBalance.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("initialFinancialBalance", initialFinancialBalance);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("initialFinancialBalance/list").addAllObjects(modelMap);
	}

	@RequestMapping("/ajaxPage")
	@DoLog(cnContent = "财务初始余额表列表ajax分页")
	@Permission(name="initialFinancialBalance.view")
	public String ajaxPage(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalanceQueryVo queryVo) throws Exception {
		try {
			Pager<InitialFinancialBalance> p = new Pager<InitialFinancialBalance>();
			String currentPage = request.getParameter(Pager.MY_PAGINATION_CURRENT_PAGE);
			p.setCurrentPage(Integer.parseInt(currentPage == null ? "1" : currentPage));
			Pager<InitialFinancialBalance> pager = initialFinancialBalanceService.search(p, queryVo);
			this.genPageJson(response, pager);
			return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toAdd")
	@DoLog(cnContent = "去到添加财务初始余额表页面")
	@Permission(name="initialFinancialBalance.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("initialFinancialBalance/add").addAllObjects(modelMap);
	}

	@RequestMapping("/add")
	@DoLog(cnContent = "添加财务初始余额表")
	@Permission(name="initialFinancialBalance.add")
	public String add(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		try {
			BusinessMap bm = initialFinancialBalanceService.create(initialFinancialBalance);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toEdit")
	@DoLog(cnContent = "去到编辑财务初始余额表页面")
	@Permission(name="initialFinancialBalance.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		InitialFinancialBalance result = initialFinancialBalanceService.getById(initialFinancialBalance.getZhangTaoId());
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("initialFinancialBalance", result);
		return new ModelAndView("initialFinancialBalance/edit").addAllObjects(modelMap);
	}

	@RequestMapping("/edit")
	@DoLog(cnContent = "编辑财务初始余额表")
	@Permission(name="initialFinancialBalance.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		try {
			BusinessMap bm = initialFinancialBalanceService.edit(initialFinancialBalance);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/view")
	@DoLog(cnContent = "查看财务初始余额表信息")
	@Permission(name="initialFinancialBalance.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		InitialFinancialBalance result = initialFinancialBalanceService.getById(initialFinancialBalance.getZhangTaoId());
		return new ModelAndView("initialFinancialBalance/view").addObject("initialFinancialBalance", result);
	}

	@RequestMapping("/delete")
	@DoLog(cnContent = "删除财务初始余额表信息")
	@Permission(name="initialFinancialBalance.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, InitialFinancialBalance initialFinancialBalance) throws Exception {
		try {
			boolean flag = initialFinancialBalanceService.delete(initialFinancialBalance.getZhangTaoId());
			return this.ajax(response, flag ? Constants.SUCCESS : Constants.ERROR);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

}
